home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 20
/
Cream of the Crop 20 (Terry Blount) (1996).iso
/
program
/
javnl006.zip
/
javnl006.txt
< prev
Wrap
Text File
|
1996-06-05
|
15KB
|
440 lines
Issue #006
June, 1996
Contents:
JavaDoc
Comparing C/C++ to Java Part 6 - Method Overloading
Introduction to Applet Programming Part 2 - Drawing
Performance - Primitive Types vs. Wrappers
JAVADOC
You may have heard of the term "JavaDoc" in relation to Java. What is
this? JavaDoc is a tool that comes with the Java Development Kit.
It's used for preparing HTML (the language of the Web) documentation
for Java classes. That is, it generates HTML code that documents a
class or package (set of related classes), including their methods,
interfaces, exceptions, class hierarchy, and so on.
JavaDoc extracts structured comments from a Java source file. For
example:
/**
* Create a hash table.
*
* @param sz initial table size in hash slots (>= 1)
* @param load_factor (average elements per slot)
* (0.25 - 10.0)
* @param growth_factor (how much to grow the table
* when load factor is exceeded) (1.1 - 2.0)
* @exception IllegalArgumentException for invalid arguments
*/
public Hash(int sz, float load_factor, float growth_factor)
{
// stuff
}
This is documentation for a constructor for a hash table class.
JavaDoc keys off of "/**" and tags like "@param". Page 182 of David
Flanagan's "Java in a Nutshell" (O'Reilly) describes the tags that can
be used.
You can run JavaDoc on individual Java source files, or on whole
packages. It generates a set of HTML files that can be used on a Web
page or with a Web browser whether connected to the Web or not. The
files have hyperlinks between related classes and methods, so that a
user can navigate easily through the documentation.
COMPARING C/C++ TO JAVA PART 6 - METHOD OVERLOADING
In both C++ and Java it's possible to have functions/methods with the
same name as other functions. For example, in C++ you can say:
class A {
public:
void f(int i) {}
void f(char* s) {}
};
and the compiler will choose the right function based on the type of
the argument given to f(). Java has a similar facility.
One question that naturally arises is how to choose the proper
function or method to call. For example, with this C++ code:
void f(short s) {}
void f(long l) {}
and a call:
char c = 0;
f(c);
it's not clear which of these functions to call, because a char could
be promoted to either a short or a long (this particular call is in
fact ambiguous in C++).
Java uses what is known as a "most specific" algorithm to figure out
which method to call. Before we explain what is meant by this, we
need to talk a bit about assignment, namely, what sorts of types can
be assigned to other types in Java. Here is an example to illustrate
this point:
public class test1 {
public static void main(String args[])
{
short s = 0;
int i = 0;
float f = 0.0f;
i = s; // OK
f = i;
s = i; // error
i = f;
s = (short)i; // OK but have to be careful
i = (int)f;
}
}
We can assign shorter primitive types to longer ones, for example
short (16 bits) to int (32 bits). We can assign an int to a float.
We cannot assign a longer type to a shorter or a float to an int,
unless we use a cast. With casting one has to be careful, because of
potential loss of precision, for example, in stuffing 32 bits into 16.
Similarly, we can assign class object references to references to a
base class, but not the other way around without casting.
Given this notion of assignment, and an actual method call, how does
Java decide which method to call? First of all, it finds all methods
that have the correct name, the correct number of parameters, and
parameters that are of types that can be assigned the values of the
arguments. If only one method matches, then that is the method that
is invoked.
If there's more than one method in the set under consideration, then
the compiler goes through and finds methods for which all the
parameter types are assignable to another method's parameter types in
the set, and removes that other method from the set of methods being
considered. The method removed is considered "less specific".
If the result of this process is a single method, then that method is
invoked. If there's more than one method left, the call is ambiguous.
To illustrate these ideas, consider another example:
public class test2 {
public static void f1(int i, int j) {}
public static void f1(int i, short j) {}
public static void f2(long i, long j) {}
public static void f2(int i, int j) {}
public static void f3(short i, short j) {}
public static void f4(double i, float j) {}
public static void f4(float i, double j) {}
public static void main(String args[])
{
int i = 0;
short s = 0;
f1(i, s);
f2(i, s);
f3(i, s);
f4(i, s);
}
}
The f1() call invokes f1(int, short) and is an exact match.
The f2() call invokes f2(int, int) because f2(int, int) has parameter
types that are assignable to f2(long, long) and f2(long, long) is
therefore removed from the set.
The f3() call is an error because an int for the first parameter
cannot be assigned to a short.
The f4() call is ambiguous because both methods can be called and
neither can be removed from the set of possible methods to call
because neither has parameter types assignable to the other.
Coming back to C++ for a moment, it's fair to say that the equivalent
argument matching rules in that language are significantly more
complicated, reflecting a more complex language in general. For
example, consider a case like:
class String {
public:
String(char);
};
where a constructor is defined to create a String from a character.
In C++, usage like:
String s(37);
is valid, with the integer constant 37 being demoted to a char and
then the constructor invoked. Whether a user intended this or not is
not clear, given that usage such as:
String s(12345);
is a problem when chars are 8 bits.
There's no magic formula for avoiding problems with method
overloading, except perhaps to follow the general rule of avoiding
cleverness. If a reader of your code can't quickly tell which method
is invoked in a given case, then it might be worth reworking the code
or at least adding a comment about its operation.
INTRODUCTION TO APPLET PROGRAMMING PART 2 - DRAWING
In previous issues we gave a simple example of an applet, and talked
about the handshaking that goes on between an applet and the applet
viewer or Web browser. As you recall, an applet is not a standalone
Java program of the sort we've presented examples of here, but instead
is a program that runs in a particular context, invoked via a Web
browser or applet viewer program.
We will continue our discussion by talking about drawing in an applet,
things like lines and boxes and colors and fonts. To illustrate these
ideas, let's look at an actual applet:
import java.applet.*;
import java.awt.*;
public class test1 extends Applet {
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(20, 20, 350, 200);
g.setColor(Color.green);
g.setFont(new Font("Helvetica", Font.ITALIC, 36));
g.drawString("This is a test", 80, 120);
//g.setColor(Color.blue);
g.setColor(new Color(0, 0, 223));